import {dictApi} from '@/api/index'
import { defineStore } from 'pinia'

export const useDictStore = defineStore('dict', () => {
    const dict = ref(new Map())

    const getDict = async (dictValues: string[]) => {
        await Promise.all(dictValues.map(async (dictValue) => {
            const { data } = await dictApi.getDictItemsByDictValueApi(dictValue)
            if (data.code == '200') {
                dict.value.set(dictValue, data.data)
                return data.data
            }
        }))
    }

    const getDictItemsLabel = (dictValue: string, dictItemValue: number|boolean) => {
        if(typeof dictItemValue === 'boolean') dictItemValue = dictItemValue ? 1 : 0
        return dict.value.get(dictValue)?.find((item: any) => item.value === dictItemValue)?.label
    }

    return {
        getDict,
        getDictItemsLabel
    }
})